{% extends 'saltstack/saltstack.html' %}
{% block title %}States模块编排{% endblock %}

<script>
function copyText()
{
document.getElementById("statev").value=document.getElementById("state").value;
}
</script>

{% block main %}

        <div class="tab-content">
                <div class="tab-pane fade in active">
                    <div class="row">
                    <br>
                        <div class="col-lg-4">
                            <form action="/saltstack/state_exec/{{ salt_server.id }}/" method="post" role="form">

                                <div class="col-lg-12">
                                    <label for="minions_up">Minion组</label>
                                    <select class="form-control" name="group" id="groupSelect2">
                                        <option  value="{{ group }}">--{{ group }}--</option>
                                        {% for group in minion_group %}
                                            <option>{{ group.groupname }}</option>
                                        {% endfor %}
                                    </select>
                                    <select size="8"  class="form-control" multiple="" name="tgt" id="minions_up">
                                    {% for i in minions %}
                                        <option>{{ i }}</option>
                                    {% empty %}
                                        <option>null</option>
                                    {% endfor %}
                                    </select>
                                </div>

                                <div class="col-lg-12">
                                    <label style="color: green">选择环境:</label>
                                    <select class="form-control" id="envSelect" name="env" >
                                        <option value="{{ env }}">--{{ env }}--</option>
                                        {% for env in envs %}
                                        <option value="{{ env }}">{{ env }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                                <div class="form-group">
                                <div class="col-lg-12">
                                    <label style="color: green">State模块:</label>
                                    <div id="states"></div>
                                    <select ondblclick="copyText()" id="state" size="8"  class="form-control" multiple="" name="state" style="background-color: black;color: green">
                                    {% for i in states %}
                                        <option>{{ i }}</option>
                                    {% endfor %}
                                    </select>
                                </div>
                                </div>
                                <div class="col-lg-12">
                                    <button  class="btn btn-primary btn-xs" type="submit">执行模块</button>
                                </div>

                            </form>

                        </div>
                        <div class="col-lg-8">
                            <ul>
                            <div class="form-group">
                                <label style="color: green">执行结果：</label>
                                <textarea rows="23" style="background-color: black;color: green" class="form-control">[root@salt-master ~]#
{% for key, value in ret.items %}
{{ key }}:
-------------------------{% for i in value.values %}
Comment:    {{ i.comment }}
Result:         {{ i.result }}
Pid:            {{ i.changes.pid }}
Retcode:        {{ i.changes.retcode }}
stderr:         {{ i.changes.stderr }}
stdout:
{{ i.changes.stdout }}
-------------------------{% endfor %}
#####################################################################
{% endfor %}</textarea>
                            </div>
                            </ul>
                        </div>



                    </div>
                </div>
            </div>

    {% block scripts %}
        <script src="/static/js/jquery-1.8.3.min.js"  type="text/javascript"></script>
{#        <script language="javascript" type="text/javascript">#}
{#            $(document).ready(function(){#}
{#            $('#envSelect').change(function(){#}
{#            var p1=$(this).children('option:selected').val();#}
{#            window.location.href="/saltstack/state_fun/{{salt_server.id}}/?env="+p1;#}
{#            })#}
{#            })#}
{#        </script>#}

        <script>
              $("#envSelect").change(function(){
                var env=$(this).children('option:selected').val();
                $.get("/saltstack/state_fun/{{salt_server.id}}",{'env':env}, function(data){
                    var array = eval(data);
                    var state = $("#state");
                    $("option",state).remove();
                    for (var i = 0; i < array.length; i++) {
                        $("#state").append("<option>" + array[i] + "</option>");
	                }
                })
              });
        </script>

        //minion组
        <script>
              $("#groupSelect2").change(function(){
                var group=$(this).children('option:selected').val();
                $.get("/saltstack/get_minion/{{salt_server.id}}/",{'group':group}, function(data){
                    var array = eval(data);
                    var minions_up = $("#minions_up");
                    $("option",minions_up).remove();
                    for (var i = 0; i < array.length; i++) {
                        $("#minions_up").append("<option>" + array[i] + "</option>");
	                }
                })
              });
        </script>
    {% endblock %}

{% endblock %}